<template>
	<view class="pl25 pr25">
		<view class="pt30 pb30 bb flex flex-ac">
			<text class="text c000">真实姓名</text>
			<input class="it1" placeholder-class="c999" placeholder="通过电子健康卡信息获取" disabled="true" v-model="userName" />
		</view>
		<view class="pt30 pb30 bb flex flex-ac">
			<text class="text c000">身份证号</text>
			<input class="it1" placeholder-class="c999" placeholder="通过电子健康卡信息获取" disabled="true" v-model="idNo" />
		</view>
		<view class="pt30 pb30 bb flex flex-ac">
			<text class="text c000">联系电话</text>
			<input class="it1" placeholder-class="c999" placeholder="通过电子健康卡信息获取" disabled="true" v-model="phone" />
		</view>
		<view class="pt30 pb30 bb flex flex-ac">
			<text class="text c000">电子健康卡</text>
			<input class="it1" placeholder-class="c999" placeholder="通过电子健康卡信息获取" disabled="true"
				v-model="healthCardNo" />
		</view>
		<view class="pt30 pb30 bb flex flex-ac flex-pj">
			<view>
				<view class="text c000">是否有过敏史</view>
				<view class="ft12 cfec322 mt5">非常重要，请如实告知</view>
			</view>
			<view class="flex flex-ac">
				<view class="flex flex-ac mr30">
					<image v-if="isAllergy==1" class="check"
						src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/checky.png">
					</image>
					<image @click="isAllergys(1)" v-else class="check"
						src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/checkn.png">
					</image>
					<text>是</text>
				</view>
				<view class="flex flex-ac mr30">
					<image v-if="isAllergy==0" class="check"
						src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/checky.png">
					</image>
					<image @click="isAllergys(0)" v-else class="check"
						src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/checkn.png">
					</image>
					<text>否</text>
				</view>
			</view>
		</view>
		<view class="pt30 pb30 bb flex" v-if="isAllergy==1">
			<text class="text c000">过敏史</text>
			<textarea placeholder="请填写过敏药物" v-model="allergyHistory"></textarea>
		</view>
		<view class="pt30 pb30 bb flex flex-ac flex-pj">
			<text class="text c000">是否有手术史</text>
			<view class="flex flex-ac">
				<view class="flex flex-ac mr30">
					<image v-if="ssState==1" class="check"
						src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/checky.png">
					</image>
					<image @click="ssStates(1)" v-else class="check"
						src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/checkn.png">
					</image>
					<text>是</text>
				</view>
				<view class="flex flex-ac mr30">
					<image v-if="ssState==0" class="check"
						src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/checky.png">
					</image>
					<image @click="ssStates(0)" v-else class="check"
						src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/checkn.png">
					</image>
					<text>否</text>
				</view>
			</view>
		</view>
		<view class="pt30 pb30 bb flex" v-if="ssState==1">
			<text class="text c000">手术史</text>
			<textarea placeholder="请填写手术史" v-model="operationHistory"></textarea>
		</view>
		<view class="pt30 pb30 bb flex flex-ac flex-pj">
			<text class="text c000">是否处于孕期</text>
			<view class="flex flex-ac">
				<view class="flex flex-ac mr30">
					<image v-if="ssyqState==1" class="check"
						src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/checky.png">
					</image>
					<image @click="ssyqStates(1)" v-else class="check"
						src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/checkn.png">
					</image>
					<text>是</text>
				</view>
				<view class="flex flex-ac mr30">
					<image v-if="ssyqState==0" class="check"
						src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/checky.png">
					</image>
					<image @click="ssyqStates(0)" v-else class="check"
						src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/checkn.png">
					</image>
					<text>否</text>
				</view>
			</view>
		</view>
		<view class="bottomBtn ftc">
			<view class="bottomBtncon bgfff">
				<text class="cfff btns ct ftc block" @click="save">完成</text>
			</view>
		</view>
		<uni-popup ref="popup" type="message">
			<uni-popup-message :type="warnType" :message=warnMessage :duration="warnTime"></uni-popup-message>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: '', //档案信息主键
				isAllergy: 0, //是否有过敏史
				allergyHistory: '', //过敏内容
				ssState: 0, //是否有手术史
				operationHistory: '', //手术内容
				ssyqState: 0, //是否孕期
				userName: '', //姓名
				idNo: '', //证件号码
				phone: '', //电话号码
				healthCardNo: '', //电子健康卡
				warnMessage: '错误', //消息内容
				warnType: 'warn', //消息类型  success/warn/info/error
				warnTime: 1000, //消息提醒延长时间
				usrUserId: null,
			};
		},
		onLoad(option) {
			var that = this;
			that.usrUserId = uni.getStorageSync("usrUserId")
			let openid=uni.getStorageSync('openId')
			if (option.id) {
				that.id = option.id;
				uni.request({
					url: 'https://app.mzmzb.com/application-usrapp/personalFile/selectPersonalFileInfo.tz',
					data: {
						id: that.id
					},
					method: "GET",
					success: (res) => {
						if (res.data.success) {
							console.log(res)
							if (res.data.result.name) {
								that.userName = res.data.result.name;
							}
							if (res.data.result.isallergy) {
								that.isAllergy = res.data.result.isallergy;
							}
							if (res.data.result.allergyhistory) {
								that.allergyHistory = res.data.result.allergyhistory;
							}
							if (res.data.result.isoperation) {
								that.ssState = res.data.result.isoperation;
							}
							if (res.data.result.ispregnancy) {
								that.ssyqState = res.data.result.ispregnancy;
							}
							if (res.data.result.operationhistory) {
								that.operationHistory = res.data.result.operationhistory;
							}
							if (res.data.result.identity) {
								that.idNo = res.data.result.identity;
							}
							if (res.data.result.mobile) {
								that.phone = res.data.result.mobile;
							}
							if (res.data.result.healthCardNo) {
								that.healthCardNo = res.data.result.healthCardNo;
							}
						}

					},
					fail(error) {

					}
				})
			} else {
				uni.getStorage({
					key: 'health_key',
					success: function(res) {
						var healthInfo = JSON.parse(res.data);
						that.userName = healthInfo.name;
						that.idNo = healthInfo.idNo;
						that.phone = healthInfo.phone;
						that.healthCardNo = healthInfo.healthCardNo;
					}
				});
			}
		},
		onShow() {},
		methods: {
			isAllergys(allergy) {
				if (typeof allergy != 'undefined') {
					this.isAllergy = allergy;
				}
			},
			ssStates(state) {
				if (typeof state != 'undefined') {
					this.ssState = state;
				}
			},
			ssyqStates(value) {
				if (typeof value != 'undefined') {
					this.ssyqState = value;
				}
			},
			save(){
				var that = this;
				let openid=uni.getStorageSync('openId')
				if(that.verification()){
					var parm = {};
					if(that.isAllergy == 0){
						that.allergyHistory = '';
					}
					if(that.ssState == 0){
						that.operationHistory = '';
					}
					parm.usrUserId = that.usrUserId;
					parm.name = that.userName;
					parm.isAllergy = that.isAllergy;
					parm.isPregnancy = that.ssyqState;
					parm.allergyHistory = that.allergyHistory;
					parm.isOperation = that.ssState;
					parm.operationHistory = that.operationHistory;
					parm.identity = that.idNo;
					parm.mobile = that.phone;
					parm.healthCardNo = that.healthCardNo;
					uni.showModal({
					    title: '提示',
					    content: '是否确定提交信息',
					    success: function (res) {
					        if (res.confirm) {
								this.fun.$request('personalFile/add.tz',parm,'POST','application/x-www-form-urlencoded').then(item=>{
									if(item.data.success){
										that.warnMessage = '保存成功';
										that.warnType = 'success';
										that.$refs.popup.open();
										setTimeout(()=>{
											//档案信息的跳转
											// uni.navigateTo({
											// 	url: '/pages/record/index?userId='+that.usrUserId
											// });
											uni.navigateBack({
												delta: 2
											});
										},1000)
									}else{
										that.warnMessage = item.data.errMsg;
										that.warnTime  = 3000;
										that.$refs.popup.open();
									}
								}).catch(error=>{
									that.warnMessage = '提交失败';
									that.warnType = 'error';
									that.$refs.popup.open();
								})
					        } else if (res.cancel) {
					            
					        }
					    }
					});
				}
			},
			//数据验证
			verification() {
				if (!this.userName || !this.phone || !this.idNo || !this.healthCardNo) {
					this.warnMessage = '请先获取电子健康卡相关信息';
					this.warnTime = 3000;
					this.$refs.popup.open();
					return false;
				}
				if (this.isAllergy == 1 && !this.allergyHistory) {
					this.warnMessage = '请输入过敏内容';
					this.$refs.popup.open();
					return false;
				}
				if (this.ssState == 1 && !this.operationHistory) {
					this.warnMessage = '请输入手术内容';
					this.$refs.popup.open();
					return false;
				}
				return true;
			},
		}
	};
</script>

<style lang="less">
	textarea {
		height: 100rpx;
	}

	.check {
		width: 28rpx;
		height: 28rpx;
		margin-right: 15rpx;
	}

	.text {
		display: inline-block;
		width: 290rpx;
	}

	.bottomBtn {
		margin-top: 30rpx;
		position: relative;
		height: 125rpx;
		width: 100%;

		.bottomBtncon {
			position: fixed;
			width: 700rpx;
			z-index: 10;
			left: 25rpx;
			height: 125rpx;
			bottom: 0;
			background: #FFFFFF;

			.btns {
				width: 660rpx;
				height: 100rpx;
				line-height: 100rpx;
				border-radius: 10rpx;
				background: #74a6fd;
			}
		}
	}
</style>